<?php

class Model_DbTable_Matricegmv extends Zend_Db_Table
{
    protected $_name = 'matricegmv';
    /**
		Recupererle point de la muraille par son id
     */
    public function getMatricegmv($id) 
    {
        $id = (int)$id;
        $row = $this->fetchRow('id = ' . $id);
        if (!$row) {
            throw new Exception("Count not find row $id");
        }
        return $row->toArray();    
    }
    
    
public function getInfoMatricegmv() {
		
		$db = Zend_Db::factory('PDO_MYSQL', array ('host' => '127.0.0.1', 'username'
		 => 'root', 'password' => '@dmin2010gmv', 'dbname'   => 'gmv'));
		$select = $db->select()
		->from(array('a' => 'matricegmv'))
		->join(array('p' => 'proprietaire'), 'p.idproprietaire = a.proprietaire_idproprietaire')
		->join(array('e' => 'espece'), 'e.idespece = a.espece_idespece')
		->join(array('o' => 'origine'), 'o.idorigine = a.origine_idorigine')
		->join(array('t' => 'terrain'), 't.idterrain = a.terrain_idterrain');
		$rows = $select->query()->fetchAll();
        $matricegmv = array();
        $matricegmv['total'] = count($rows);
        $matricegmv['rows'] = $rows;
		return $matricegmv;    
	}
	

	
    public function addMatricegmv($i,$j,$type,$longueur,$taille,$terrain,$proprietaire,$origine,$espece,$latitude,$longitude)
    {
            $data = array(
            'i' => $i,
        	'j' => $j,
            'longueur' => $longueur,
			'taille' => $taille,
			//'terrain_idterrain' => $terrain,
			'proprietaire_idproprietaire' => $proprietaire,
			'origine_idorigine' => $origine,
			'espece_idespece' => $espece,
			'latitude' => $latitude,
			'longitude' => $longitude,
        	'type' => $type,
        );
        $this->insert($data);
    }


    
	public function updateMatricegmv($id, $i, $j, $type, $longueur,$taille,$terrain,$proprietaire,$origine,$espece,$latitude,$longitude)
		{
		$data = array(
		    'i' => $i,
        	'j' => $j,
        	'type' => $type,
			'longueur' => $longueur,
			'taille' => $taille,
			'terrain_idterrain' => $terrain,
			'proprietaire_idproprietaire' => $proprietaire,
			'origine_idorigine' => $origine,
			'espece_idespece' => $espece,
			'latitude' => $latitude,
			'longitude' => $longitude,
		);
		$this->update($data, 'id = '. (int)$id);
		}
		
	function deleteMatricegmv($id) {
		$this->delete ( 'id =' . ( int ) $id );
	}
    /**
     * 
     * @return  
     */
    public function getAllMatricegmv() {    
        $all = $this->fetchAll();
        $matricegmv = array();
        $matricegmv['total'] = count($all->toArray());
        $matricegmv['rows'] = $all->toArray();

        
        return $matricegmv ;
    }    
    
  public function getLatitude($id) {    
		$select = $this -> select() -> from('matricegmv',array('latitude'))
			-> where('id = ? ',$id)
			;
		
		$result = $this -> fetchRow($select);
		return $result->toArray() ;
	}   
	
	public function getLongitude($id) {    
		$select = $this -> select() -> from('matricegmv',array('longitude'))
			-> where('id = ? ',$id)
			;
		
		$result = $this -> fetchRow($select);
		return $result->toArray() ;
	}  
	
	
public function getCoordonneee($imin,$imax,$jmin,$jmax) {    
 		$select = $this ->select()
             -> from('matricegmv')
             -> where('i > ?', $imin)
             -> where('i <= ?', $imax)
             -> where('j > ?', $jmin)
             -> where('j <= ?', $jmax);
		$all = $this -> fetchAll($select);
        $matricegmv = array();
        $matricegmv['total'] = count($all->toArray());
        $matricegmv['rows'] = $all->toArray();
        //$result = $this -> fetchAll($select);
        return $matricegmv;
    }

    
   
    
}
